﻿Imports System.Runtime.InteropServices
Imports System.IO

Module Module1

    Dim strOriginalAssemblyFile As String
    Dim strNewLocation As String

    ''' <summary>
    ''' This program will copy an assembly and all linked documents to a new folder. 
    ''' The folder structure will be maintained, however, if a linked document is not in the 
    ''' same folder or sub folder as the parent assembly it will not be moved.
    ''' 
    ''' This program will accept two command line argument:
    ''' 1st: An assembly to move (Full file name, including extension)
    ''' 2nd: A new location for the assembly (A directory name, if the directory does not exist it will be created)
    ''' 
    ''' Sample Usage: MoveDocuments "c:\Models\Model.asm" "c:\NewFolder\"
    ''' </summary>
    Sub Main()
        Dim objApplication As RevisionManager.Application = Nothing
        Dim objDocument As RevisionManager.Document = Nothing

        Try
            'set the member variables to the passed arguments
            strOriginalAssemblyFile = System.Environment.GetCommandLineArgs(1)
            strNewLocation = System.Environment.GetCommandLineArgs(2)

            If Not File.Exists(strOriginalAssemblyFile) Then
                Throw New ApplicationException(strOriginalAssemblyFile & " File does not exist")
            End If

            If Not Directory.Exists(strNewLocation) Then
                'try creating the directory, if the path provided is invalid this will throw an exception
                Directory.CreateDirectory(strNewLocation)
            End If

        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine("Press any key to close.")
            Console.ReadKey()
            Exit Sub
        End Try

        Try
            ' Create new instance of the Revision Manager Application object 
            objApplication = New RevisionManager.Application
            objApplication.DisplayAlerts = False

            'Open the file with the Revision Manager API
            objDocument = objApplication.Open(strOriginalAssemblyFile)

            'Call the recursive function to move this doc and all linked docs
            MoveDocAndAllLinkedFiles(objDocument, Path.GetDirectoryName(strOriginalAssemblyFile))

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        Finally
            If Not (objDocument Is Nothing) Then
                Marshal.ReleaseComObject(objDocument)
                objDocument = Nothing
            End If
            If Not (objApplication Is Nothing) Then
                objApplication.Quit()
                Marshal.ReleaseComObject(objApplication)
                objApplication = Nothing
            End If
        End Try

        Console.WriteLine("Press any key to close.")
        Console.ReadKey()

    End Sub


    Sub MoveDocAndAllLinkedFiles(ByVal aDocument As RevisionManager.Document, ByVal aOriginalFileDirectory As String)
        Try
            'find the new directory name, keeping the sub folders
            Dim oldFileInfo As New FileInfo(aDocument.FullName)
            Dim newDirectoryName As String = oldFileInfo.DirectoryName.Replace(aOriginalFileDirectory, strNewLocation)
            Dim newFileName As String = Path.Combine(newDirectoryName, oldFileInfo.Name)

            'create the directory if it does not already exsist.
            If Not Directory.Exists(newDirectoryName) Then
                Directory.CreateDirectory(newDirectoryName)
            End If

            Console.WriteLine("Copying [" & aDocument.FullName & "] to [" & newFileName & "]")
            Console.WriteLine()

            aDocument.Copy(newFileName)

            If aDocument.LinkedDocuments.Count > 0 Then

                For i = 1 To aDocument.LinkedDocuments.Count
                    Dim objLinkedDocument As RevisionManager.Document = aDocument.LinkedDocuments.Item(i)
                    Try
                        'make sure this document has not already been moved. 
                        'since Solid Edge documents can have circular links
                        If Not objLinkedDocument.FullName.StartsWith(strNewLocation) Then
                            MoveDocAndAllLinkedFiles(objLinkedDocument, aOriginalFileDirectory)
                        End If
                    Catch ex As Exception
                        Console.WriteLine(ex.Message)
                    Finally 'clean up
                        If Not (objLinkedDocument Is Nothing) Then
                            Marshal.ReleaseComObject(objLinkedDocument)
                            objLinkedDocument = Nothing
                        End If
                    End Try
                Next

            End If


        Catch ex As Exception
            Console.WriteLine(ex.Message)

        Finally
            If Not (aDocument Is Nothing) Then
                Marshal.ReleaseComObject(aDocument)
                aDocument = Nothing
            End If
        End Try


    End Sub

End Module